<!-- 产品特色-- linghui -- 2017年7月6日 -->
<template>
  <el-row>
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;height:46px">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="featuresNameCondition" placeholder="产品特色名称" id="inputheight"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search" size="small" style="line-height:14px">查询</el-button>
        </el-form-item>
        <el-form-item class="button-align">
          <el-button type="primary" @click="handleAdd" size="small">新增产品特色</el-button>
        </el-form-item>
        <el-form-item class="button-align">
          <el-button type="danger" @click="batchDelete" :disabled="this.sels.length===0" size="small">批量删除</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <el-table :data="productFeaturesList" v-loading="listLoading" @selection-change="selsChange" border
              style="width: 100%; font-size: 12px;" row-class-name="tabRow">
      <el-table-column type="selection"></el-table-column>
      <el-table-column align="center" prop="featuresName" label="产品特色名称"></el-table-column>
      <el-table-column align="center" :formatter="createTimeToStr" width="200" label="创建时间"></el-table-column>
      <el-table-column align="center" width="80" label="操作">
        <template scope="scope">
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="toolbar" style="margin-top: 15px;" v-show="!listLoading">
      <el-pagination id="pagination" layout="total, sizes, prev, pager, next, jumper" :page-sizes="[10, 20, 30]"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增/修改弹出层-->
    <el-dialog :title="dialogTitle" v-model="showAdd" :close-on-click-modal="false" @close="resetPassenger">
      <el-form :model="productFeatures" label-width="110px" :rules="productFeaturesFormRules" ref="productFeatures"
               style="margin-top: -30px;">
        <el-form-item label="产品特色名称" prop="featuresName" id="formheight">
          <el-input v-model="productFeatures.featuresName"></el-input>
        </el-form-item>
      </el-form>
      <div scope="footer" class="dialog-footer" style="float: right;margin-bottom: 20px;">
        <el-button @click.native="resetPassenger">取消</el-button>
        <el-button type="primary" @click.native="handleSubmit" :loading="saveLoading">保存</el-button>
      </div>
    </el-dialog>
  </el-row>
</template>

<script type="text/ecmascript-6">
  import {sessionOut} from '../../smartlight/common/js/util.js'
  export default{
    data () {
      return {
        saveLoading: false,
        total: 2,
        page: 1,
        pageSize: 10,
        productFeaturesList: [],
        count: 0,
        sels: [], // 列表选中列
        companyId: 0,
        loginType: 0,
        showAdd: false,
        listLoading: false,
        dialogTitle: '新增产品特色',
        loginType1: {
          type: 0
        },
        productFeatures: {
          'featuresName': ''
        },
        featuresNameCondition: '',
        productFeaturesFormRules: {
          featuresName: [
            {required: true, message: '请输入产品特色名称', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      checkIfLogin () { // 判断是否登录超时
        var user = sessionStorage.getItem('user')
        if (!user) {
          this.$alert('登录超时，请重新登录！', ' ', {
            type: 'warning'
          }).then(() => {
            this.$router.push({path: '/login'})
            sessionStorage.clear()
          })
        } else {
          return true
        }
      },
      // 分页插件查询方法-条数
      handleSizeChange (val) {
        this.pageSize = val
        this.initProductFeatureList()
      },
      // 分页插件查询方法-页数
      handleCurrentChange (val) {
        this.page = val
        this.currentPage = val
        this.initProductFeatureList()
      },
      // 【查询】
      search () {
        if (!this.checkIfLogin()) {
          return
        }
        this.page = 1
        this.initProductFeatureList()
      },
      selsChange: function (sels) {
        this.sels = sels
      },
      // 初始化分页查询常旅客信息
      initProductFeatureList () {
        this.listLoading = true
        var para = {
          pageNumber: this.page,
          pageSize: this.pageSize,
          featuresName: this.featuresNameCondition.trim(),
          type: this.loginType
        }
        this.$http.post('/tour/web/productFeatures/getList.htm', para).then(response => {
          var resBody = response.body
          if (resBody.isSuccess === 0) {
            this.total = resBody.result.totalElements
            this.productFeaturesList = resBody.result.content
            this.listLoading = false
          } else if (resBody.isSuccess === 2) {
            this.listLoading = false
            sessionOut(this) // session超时处理
          } else {
            this.$message({
              showClose: true,
              message: resBody.errorMsg,
              type: 'error'
            })
          }
        })
      },
      createTimeToStr (row, column) {
        let str = this.createTimeStr(row.createTime)
        return str
      },
      //      时间格式化
      createTimeStr (time) {
        var d = new Date(time)
        var year = d.getFullYear()
        var month = d.getMonth() + 1
        if (month < 10) {
          month = '0' + month
        }
        var date = d.getDate()
        if (date < 10) {
          date = '0' + date
        }
        var hh = d.getHours()
        if (hh < 10) {
          hh = '0' + hh
        }
        var mm = d.getMinutes()
        if (mm < 10) {
          mm = '0' + mm
        }
        var ss = d.getSeconds()
        if (ss < 10) {
          ss = '0' + ss
        }
        return year + '-' + month + '-' + date + ' ' + hh + ':' + mm + ':' + ss
      },
      handleSubmit () {
        if (!this.checkIfLogin()) {
          return
        }
        this.$refs.productFeatures.validate((valid) => {
          if (valid) {
            var url = '/tour/web/productFeatures/save.htm'
            var para = {
              productFeatures: this.productFeatures,
              type: this.loginType
            }
            this.saveLoading = true
            this.$http.post(url, para).then(response => {
              this.saveLoading = false
              var resBody = response.body
              if (resBody.isSuccess === 0) {
                this.showAdd = false
                this.$message({
                  showClose: true,
                  message: '新增产品特色成功',
                  type: 'success'
                })
                this.initProductFeatureList()
              } else if (resBody.isSuccess === 2) {
                this.listLoading = false
                sessionOut(this) // session超时处理
              } else {
                this.$message({
                  showClose: true,
                  message: resBody.errorMsg,
                  type: 'error'
                })
              }
            })
          }
        })
      },
      handleAdd () {
        this.showAdd = true
      },
      // 【删除】按钮事件
      handleDelete (index, row) {
        var ids = []
        ids.push(row.id)
        this.deleteProductFeatures(ids)
      },
      // 【批量删除】按钮事件
      batchDelete () {
        var ids = []
        this.sels.map(item => {
          ids.push(item.id)
        })
        this.deleteProductFeatures(ids)
      },
      // 【删除】、【批量删除】执行方法
      deleteProductFeatures (ids) {
        if (!this.checkIfLogin()) {
          return
        }
        this.$confirm('确定删除产品特色吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.$http.post('/tour/web/productFeatures/deleteByIds.htm', ids + '|' + this.loginType).then(response => {
            var resBody = response.body
            if (resBody.isSuccess === 0) {
              this.$message({
                showClose: true,
                message: '删除成功',
                type: 'success'
              })
              this.page = 1
              this.initProductFeatureList()
            } else if (resBody.isSuccess === 2) {
              this.listLoading = false
              sessionOut(this) // session超时处理
            } else {
              this.$message({
                showClose: true,
                message: '删除失败',
                type: 'error'
              })
            }
          })
        }).catch(() => {})
      },
      // 【取消】，关闭弹出层
      resetPassenger () {
        this.showAdd = false
        this.productFeatures = {
          featuresName: ''
        }
        this.$refs['productFeatures'].resetFields()
      }
    },
    mounted () {
      var user = sessionStorage.getItem('user')
      if (user) {
        user = JSON.parse(user)
        this.loginType = user.type
        this.companyId = user.companyId
        this.loginType1.type = user.type
        this.initProductFeatureList()
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
  .tabRow
    font-size: 12px;

  .button-align
    float: right;

  .el-dialog--small
    width: 37%;

  .prompt
    color: #ff4949
    line-height: 10px !important;
    display: block;

  #formheight
    margin: 14px 0;
    height: 38px;
    .el-form-item__content
      height: 30px;
      .el-form-item__error
        margin-top: 3px
    .el-input
      height: 30px;
      .el-input__inner
        height: 30px;

  .borderStyle
    .el-input .el-input__inner {
      border-color: #ff4949 !important;
    }

  .cell
    padding-left: 0px !important
    padding-right 0px !important
    overflow: hidden !important
    text-overflow: ellipsis !important
    white-space: nowrap !important
    .el-checkbox {
      padding-left: 15px !important
    }

  #pagination
    background-color: transparent
    .el-input__inner
      width: 120px
</style>
